<?php
add_action('widgets_init', 'register_top_category_home_widget'); // load widget

function register_top_category_home_widget() { // register widget
    register_widget('top_category_home');
}

class top_category_home extends WP_Widget {// widget class {

    function top_category_home() { 
    // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Lastest news-w', 'description' =>
            'Display top Category Home page');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'top_category_home');
        $this->WP_Widget('top_category_home', 'Coupon 6789 - top Category Home page', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        global $wpdb,$table_prefix;
        $title = apply_filters('widget_title', $instance['title']);
        echo $title;
        $categories = get_terms('store_category',array('hide_empty'=> false));
        foreach ($categories as $categorys){
            $term_link = get_term_link( $categorys, 'store_category' );
            echo '<div><h4><a href="'.$term_link.'">'.$categorys->term_id.'=>'.$categorys->name.'</a></h4></div>';
        }
        
    }

    function update($new_instance, $old_instance) { // update widget
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        return $instance;
    }

    function form($instance) { // form for the widget options
        ?>
        <div style="color: #333;">
            <p>        
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
            </p>                 
        </div>
        <?php
    }
}
?>
